package com.ruoyi.biz.letter.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.biz.attchment.domain.BizAttachment;
import com.ruoyi.biz.attchment.service.IBizAttachmentService;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.biz.letter.util.AcceptDeptUtil;
import com.ruoyi.biz.letter.util.CategoryUtil;
import com.ruoyi.common.enums.AttachementType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.letter.mapper.BizRecordLetterMapper;
import com.ruoyi.biz.letter.domain.BizRecordLetter;
import com.ruoyi.biz.letter.service.IBizRecordLetterService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 督办事项登记Service业务层处理
 *
 * @author Fei.Cong
 * @date 2024-07-11
 */
@Service
public class BizRecordLetterServiceImpl extends ServiceImpl<BizRecordLetterMapper, BizRecordLetter> implements IBizRecordLetterService {

    @Autowired
    private BizRecordLetterMapper bizRecordLetterMapper;

    @Autowired
    private IBizCategoryService bizCategoryService;

    @Autowired
    private IBizAttachmentService bizAttachmentService;

    /**
     * 分页查询督办事项登记
     *
     * @param page
     * @param bizRecordLetter 督办事项登记
     */
    @Override
    public IPage<BizRecordLetter> pageList(BizRecordLetter bizRecordLetter, IPage<BizRecordLetter> page) {
        IPage<BizRecordLetter> pages = bizRecordLetterMapper.selectPageList(page, bizRecordLetter);
        for (BizRecordLetter record : pages.getRecords()) {
            record.setFirstCategoryName(CategoryUtil.getCacheCategory(record.getPCategoryId()));
            record.setSecondCategoryName(CategoryUtil.getCacheCategory(record.getSCategoryId()));
            AcceptDeptUtil.parseAcceptDept(record);
        }
        return pages;
    }

    /**
     * 条件查询督办事项登记
     *
     * @param bizRecordLetter 督办事项登记
     * @return
     */
    @Override
    public List<BizRecordLetter> queryList(BizRecordLetter bizRecordLetter) {
        LambdaQueryWrapper<BizRecordLetter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BizRecordLetter::getSource, bizRecordLetter.getSource());
        return bizRecordLetterMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void add(BizRecordLetter bizRecordLetter) {
        CategoryUtil.buildCategoryId(bizRecordLetter);
        AcceptDeptUtil.buildAcceptDept(bizRecordLetter);
        bizRecordLetter.setLetterTime(Objects.nonNull(bizRecordLetter.getLetterTime()) ? bizRecordLetter.getLetterTime() : new Date());
        bizRecordLetterMapper.insert(bizRecordLetter);
        addAttachment(bizRecordLetter);
        addAttachment1(bizRecordLetter);
    }

    @Override
    public void edit(BizRecordLetter bizRecordLetter) {
        CategoryUtil.buildCategoryId(bizRecordLetter);
        AcceptDeptUtil.buildAcceptDept(bizRecordLetter);
        bizRecordLetterMapper.updateById(bizRecordLetter);
        addAttachment(bizRecordLetter);
        addAttachment1(bizRecordLetter);
    }

    private void addAttachment(BizRecordLetter bizRecordLetter) {
        if (StrUtil.isNotBlank(bizRecordLetter.getAttachments())) {
            List<BizAttachment> bizAttachmentList = Arrays.stream(bizRecordLetter.getAttachments().split(",")).map(v -> {
                BizAttachment attachment = new BizAttachment();
                attachment.setFilePath(v);
                attachment.setFileName(AttachementType.LETTER_ATTACHEMENT.getDesc());
                return attachment;
            }).collect(Collectors.toList());
            bizAttachmentService.batchAdd(bizAttachmentList, bizRecordLetter.getId(), bizRecordLetter.getCreateBy(), AttachementType.LETTER_ATTACHEMENT);
        } else {
            bizAttachmentService.remove(new LambdaQueryWrapper<BizAttachment>().eq(BizAttachment::getApplyId, bizRecordLetter.getId())
                    .eq(BizAttachment::getAttachmentType, AttachementType.LETTER_ATTACHEMENT.getCode()));
        }
    }

    private void addAttachment1(BizRecordLetter bizRecordLetter) {
        if (StrUtil.isNotBlank(bizRecordLetter.getAttachments1())) {
            List<BizAttachment> bizAttachmentList = Arrays.stream(bizRecordLetter.getAttachments1().split(",")).map(v -> {
                BizAttachment attachment = new BizAttachment();
                attachment.setFilePath(v);
                attachment.setFileName(AttachementType.LETTER_ORIGIN_ATTACHEMENT.getDesc());
                return attachment;
            }).collect(Collectors.toList());
            bizAttachmentService.batchAdd(bizAttachmentList, bizRecordLetter.getId(), bizRecordLetter.getCreateBy(), AttachementType.LETTER_ORIGIN_ATTACHEMENT);
        } else {
            bizAttachmentService.remove(new LambdaQueryWrapper<BizAttachment>().eq(BizAttachment::getApplyId, bizRecordLetter.getId())
                    .eq(BizAttachment::getAttachmentType, AttachementType.LETTER_ORIGIN_ATTACHEMENT.getCode()));
        }
    }


}
